package com.morningstar.qs.api.basic.common.encrypt.rsa;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.List;
import java.util.Map;


import com.morningstar.qs.api.basic.common.config.GlobalConfig;
import com.mstar.qs.common.Entry;



public class SDKEncrypt {
	private final String path;
	private static final String Secret_Dic = "directory";
	private static final String Secret_Config_Node = "secret-key";
	private static final String SDK_Config_Node = "sdk-config";
	private static SDKEncrypt uniqueInstance = null;
	private SDKEncrypt(){
		Map<String,List<Entry>> SDKentry= GlobalConfig.entries(SDK_Config_Node).get(0).getEntries();//should get from configuration file
		if(SDKentry.containsKey(Secret_Config_Node)){
			path = SDKentry.get(Secret_Config_Node).get(0).attribute(Secret_Dic);
		}else{
			path = "/data/conf/rsa";
		}
	}
	public static SDKEncrypt getInstance(){
		if(uniqueInstance==null){
			uniqueInstance = new SDKEncrypt();
		}
		return uniqueInstance;
	}
	public String encrypt(String source){
		String ensource = "";
		try{
			ensource = SecUtil.encrypt((RSAPublicKey)GenKey.genKey(path, false), source);
		}catch(Exception e){
			e.printStackTrace();
		}
		return ensource;
	}
	public String decrypt(String source){
		String desource="";
		try{
			desource = SecUtil.decrypt((RSAPrivateKey)GenKey.genKey(path, true), source);
		}catch(Exception e){
			e.printStackTrace();
		}
		return desource;
	}
	
	public  static void main(String[] args){
		try {
			GlobalConfig.configure("E:/SVN/data/conf/dev.xml");
			System.out.print(SDKEncrypt.getInstance().encrypt("632C7E82B588F5FD2379BE9AA2B45725B9508F1064BA4C2AF3E5FB7134B0130A81D5738737DE7417497833BB4DA8BFA8266FE6729B9ED414919F872AA1A31210"));//("Abc123*DISN"));
			//new DownLoadService().zipAndDownLoad(file, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
